//
//  SLocalUtil.swift
//  本地存储
//
//  Created by BrightLi on 2021/2/12.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import Foundation

@objcMembers public class SLocalUtil:NSObject
{
    dynamic public class func hasKey(_ forKey:String)->Bool
    {
        let def:UserDefaults = UserDefaults.standard
        let temp = def.object(forKey: forKey)
        return  temp != nil
    }
    dynamic public class func removeKey(_ forKey:String)
    {
        let def:UserDefaults = UserDefaults.standard
        def.removeObject(forKey: forKey)
    }
    
    dynamic public class func removePrefixKey(_ forKey:String)
    {
        let def:UserDefaults = UserDefaults.standard
        for key in def.dictionaryRepresentation().keys{
            if key.hasPrefix(forKey){
                removeKey(key)
            }
        }
    }
    
    dynamic public class func setInt(_ value:Int,forKey:String)
    {
        if forKey.count<1
        {
            return
        }
        let def = UserDefaults.standard
        def.set(value, forKey: forKey)
    }
    
    dynamic public class func setBool(_ value:Bool,forKey:String)
    {
        if forKey.count<1
        {
            return
        }
        let def = UserDefaults.standard
        def.set(value,forKey:forKey)
    }
    
    dynamic public class func arrayForKey(_ key:String) -> Array<Any>
    {
        if key.count<1
        {
            return []
        }
        let def = UserDefaults.standard
        if let result = def.array(forKey: key){
            return result
        }
        return []
    }
    
    dynamic public class func addToArray(_ key:String,target:Any)
    {
        if key.count<1
        {
            return
        }
        var result = self.arrayForKey(key)
        result.append(target)
        let def = UserDefaults.standard
        def.set(result,forKey:key)
    }
    
    dynamic public class func boolForKey(_ key:String) -> Bool
    {
        if key.count<1
        {
            return false
        }
        let def = UserDefaults.standard
        return def.bool(forKey: key)
    }
    dynamic public class func setStringN(_ value:String,forKey:String)
    {
        if forKey.count<1
        {
            return
        }
        let def:UserDefaults = UserDefaults.standard
        def.set(value,forKey:forKey)
    }
    
    dynamic public class func setString(_ value:String,forKey:String)
    {
        if value.count<1 || forKey.count<1
        {
            return
        }
        let def:UserDefaults = UserDefaults.standard
        def.set(value,forKey:forKey)
    }
    
    dynamic public class func setDict(_ value:[String:Any],forKey:String)
    {
        if value.count<1 || forKey.count<1
        {
            return
        }
        let def:UserDefaults = UserDefaults.standard
        def.set(value,forKey:forKey)
    }
    
    dynamic public class func setObj(_ value:NSObject,forKey:String)
    {
        if forKey.count<1
        {
            return
        }
        let def:UserDefaults = UserDefaults.standard
        def.set(value,forKey:forKey)
    }
    
    dynamic public class func intForKey(_ key:String,valid:Int = 0) -> Int
    {
        if key.count<1
        {
            return valid
        }
        if !hasKey(key){
            return valid
        }
        let result = SDataUtil.intOf(UserDefaults.standard.integer(forKey:key),valid:valid)
        return result
    }
    
    dynamic public class func stringForKey(_ key:String) -> String
    {
        if key.count<1
        {
            return ""
        }
        let result = SDataUtil.stringOf(UserDefaults.standard.string(forKey: key))
        return result
    }
    
    dynamic public class func stringForKey(_ key:String,valid:String) -> String
    {
        if key.count<1
        {
            return valid
        }
        let result = SDataUtil.stringOf(UserDefaults.standard.string(forKey: key),valid:valid)
        return result
    }
    
    dynamic public class func objForKey(_ key:String) -> NSObject?
    {
        if key.count<1
        {
            return nil
        }
        if let value = (UserDefaults.standard.object(forKey: key)){
            if let dict = (value as? NSObject){
                return dict
            }
        }
        return nil
    }
    
    dynamic public class func dictForKey(_ key:String) -> [String:Any]?
    {
        if key.count<1
        {
            return nil
        }
        if let value = (UserDefaults.standard.object(forKey: key)){
            if let dict = (value as? [String:Any]){
                return dict
            }
        }
        return nil
    }
    
    dynamic public class func jsonOf(_ file:String) -> Any?{
        let path = Bundle.main.path(forResource:file,ofType:"json") ?? ""
        if path.count<1{
            return nil
        }
        let data = NSData(contentsOfFile:path) as Data?
        let json = SDataUtil.jsonOfData(data)
        return json
    }
}
